﻿using System;
using System.Collections.Generic;
using ParserGenerator.GrammarConstruction;

namespace ParserGenerator.ParserConstruction
{
    [Serializable]
    public sealed class ParserAutomatonShift : ParserAutomatonContribution
    {
        public GrammarTerminalSymbol Symbol { get; }
        public ParserAutomatonState Target { get; }

        internal ParserAutomatonShift(ParserAutomatonState state, GrammarTerminalSymbol symbol, ParserAutomatonState target)
            : base(state)
        {
            if (symbol == null)
                throw new ArgumentNullException(nameof(symbol));
            if (target == null)
                throw new ArgumentNullException(nameof(target));
            Symbol = symbol;
            Target = target;
        }

        public List<ParserAutomatonItem> GetRelatedItems()
        {
            List<ParserAutomatonItem> list = new List<ParserAutomatonItem>();
            ParserAutomatonItemSet closure = State.Core.ComputeClosure(true);
            foreach (ParserAutomatonItem item in closure)
                if (item.IsShift && item.Symbol == Symbol)
                    list.Add(item);
            return list;
        }

        public sealed override string ToString()
        {
            return "shift to " + Target.Number;
        }
    }
}
